package com.driver.core;

import com.driver.core.mapper.annotation.Order;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ReflectUtil {




    public static void getMapperMethod(StringBuilder sb,  Class zclass, Object obj) {
        List<MethodOrder> methodOrders = new ArrayList<>();
        Method[] methods = zclass.getMethods();//父类的方法也可以获取
        for (Method method : methods) {
            Order annotation = method.getAnnotation(Order.class);
            if (annotation != null) {
                methodOrders.add(new MethodOrder(annotation.order(), method.getName()));
            }
        }

        Collections.sort(methodOrders);//按order排序
        for (MethodOrder methodOrder : methodOrders) {
            try {
                Method method = zclass.getMethod(methodOrder.methodName);
                sb.append(method.invoke(obj));
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }


    }

    static class MethodOrder implements Comparable<MethodOrder> {
        private Integer order;
        private String methodName;

        public MethodOrder(Integer order, String methodName) {
            this.order = order;
            this.methodName = methodName;
        }

        @Override
        public int compareTo(MethodOrder o) {
            return this.order - o.order;
        }


    }
}
